//
//  MDUser.m
//  evidenceGather
//
//  Created by 王继伟 on 13-8-21.
//  Copyright (c) 2013年 无. All rights reserved.
//

#import "MDUser.h"
#import "UserVO.h"
 

@implementation MDUser

//@property (retain,nonatomic) NSString *pkUser;    //用户主键
//@property (retain,nonatomic) NSString *loginuser;    //登录帐号
//@property (retain,nonatomic) NSString *pwd;     //登录密码
//@property (retain,nonatomic) NSString *serverIP;     //服务器ip
//@property (retain,nonatomic) NSString *serverPort;     //服务器端口
//@property (nonatomic) int alamMinSpace;    //最小空间预警值

//数据库插入记录对象
-(NSString *)insertUserVO:(UserVO *) userVO{
    NSDate *  loginTime=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *  timeString=[dateformatter stringFromDate:loginTime];

    NSString *insertSQL=[[NSString alloc] initWithFormat:@"insert into users(loginuser,pwd,serverIP,serverPort,alamMinSpace,lastLoginTime) values('%@','%@','%@','%@','%@','%@');",userVO.loginuser,userVO.pwd,userVO.serverIP,userVO.serverPort,userVO.alamMinSpace,timeString];
    //NSLog(@"the insert sql is %@",insertSQL);
    [super.dbDelegate executeUpdate:insertSQL];
    return userVO.pkUser;
}

//更新用户信息
-(void) updateUser:(UserVO *) userVO{
    NSDate *  loginTime=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *  timeString=[dateformatter stringFromDate:loginTime];
    
    
    NSString *insertSQL=[[NSString alloc] initWithFormat:@"update users set pwd='%@',serverIP='%@',serverPort='%@',alamMinSpace='%@',lastLoginTime='%@' where loginuser='%@'" ,userVO.pwd,userVO.serverIP,userVO.serverPort,userVO.alamMinSpace,timeString,userVO.loginuser];
    //NSLog(@"the insert sql is %@",insertSQL);
    [super.dbDelegate executeUpdate:insertSQL]; 
}


//根据用户查询项目
-(UserVO *)qryUserByUser:(NSString *) pkUser{
    UserVO *retvo = nil;
    if(nil != pkUser && [pkUser length] > 0){
        NSMutableString *sql = [[NSMutableString alloc]initWithString:@"select loginuser,pwd,serverIP,serverPort,alamMinSpace from users where loginuser='"];
        [sql appendString:pkUser];
        [sql appendString:@"';"];
        
        NSArray *rs = [super.dbDelegate executeQuery:sql];
        //当结果集内无对应的Bug记录时
        if(nil != rs && [rs count] == 1){
            NSDictionary *dic = [rs objectAtIndex:0];
            if(nil == retvo) {
                retvo = [[UserVO alloc]init];
                retvo.pkUser = [[NSString alloc] initWithData:[dic objectForKey:@"pkUser"] encoding:NSUTF8StringEncoding];
                retvo.loginuser = [[NSString alloc] initWithData:[dic objectForKey:@"loginuser"] encoding:NSUTF8StringEncoding];
                retvo.pwd = [[NSString alloc] initWithData:[dic objectForKey:@"pwd"] encoding:NSUTF8StringEncoding];
                retvo.serverIP = [[NSString alloc] initWithData:[dic objectForKey:@"serverIP"] encoding:NSUTF8StringEncoding];
                retvo.serverPort = [[NSString alloc] initWithData:[dic objectForKey:@"serverPort"] encoding:NSUTF8StringEncoding];
                retvo.alamMinSpace = [[NSString alloc] initWithData:[dic objectForKey:@"alamMinSpace"] encoding:NSUTF8StringEncoding];
            }
        }
        [rs release];
        [sql release];
    }
    return retvo;
}

-(UserVO *) qryUserLastLogin
{
    UserVO *retvo = nil;

        NSMutableString *sql = [[NSMutableString alloc]initWithString:@"select loginuser,pwd,serverIP,serverPort,alamMinSpace from users order by lastLoginTime desc"];
        
        
        NSArray *rs = [super.dbDelegate executeQuery:sql];
        //当结果集内无对应的Bug记录时
        if(nil != rs && [rs count] > 0){
            NSDictionary *dic = [rs objectAtIndex:0];
            if(nil == retvo) {
                retvo = [[UserVO alloc]init];
                retvo.pkUser = [[NSString alloc] initWithData:[dic objectForKey:@"pkUser"] encoding:NSUTF8StringEncoding];
                retvo.loginuser = [[NSString alloc] initWithData:[dic objectForKey:@"loginuser"] encoding:NSUTF8StringEncoding];
                retvo.pwd = [[NSString alloc] initWithData:[dic objectForKey:@"pwd"] encoding:NSUTF8StringEncoding];
                retvo.serverIP = [[NSString alloc] initWithData:[dic objectForKey:@"serverIP"] encoding:NSUTF8StringEncoding];
                retvo.serverPort = [[NSString alloc] initWithData:[dic objectForKey:@"serverPort"] encoding:NSUTF8StringEncoding];
                retvo.alamMinSpace = [[NSString alloc] initWithData:[dic objectForKey:@"alamMinSpace"] encoding:NSUTF8StringEncoding];
            }
        }
        [rs release];
        [sql release];
    
    return retvo;
}

@end
